$(function () {

    var html_data = {
        baseConfig: baseConfig,
        un_pay_order_index: 1,
        un_pay_order_count: 0,
        un_pay_order_list: [],
        pay_order_index: 1,
        pay_order_count: 0,
        pay_order_list: [],
        goods_order_index: 1,
        goods_order_count: 0,
        goods_order_list: [],
        sessionid: '',
        sessionid_q: '',
        address_info: {},
        province_list: [],
        city_list: [],
        county_list: [],
        address_list: []
    };
    var app = new Vue({
        el: '#user_order_page',
        data: html_data,
        methods: {
            price_filter: price_filter,
            query_un_pay_order_info_list: function () {
                api.lorder.query_pay_order_info_list({
                    charge_status: '0,1,3',
                    page_index: html_data.un_pay_order_index
                }).then(function (data) {
                    html_data.un_pay_order_index = data.result.page_index;
                    html_data.un_pay_order_count = data.result.page_count;
                    if (html_data.un_pay_order_index != data.result.page_count) {
                        html_data.un_pay_order_index += 1;
                    }
                    _(data.result.list).each(function (item) {
                        html_data.un_pay_order_list.push(item);
                        item['goods_num'] = 0;
                        _(item.goods_order_list).each(function (order) {
                            _(order.goods_order_item_list).each(function (goods) {
                                item['goods_num'] += goods.num;

                            });
                        });

                    });
                });
            },
            query_pay_order_info_list: function () {
                api.lorder.query_finish_goods_order_list({
                    page_size: 200,
                    page_index: html_data.pay_order_index
                }).then(function (data) {
                    html_data.pay_order_index = data.result.page_index;
                    html_data.pay_order_count = data.result.page_count;
                    if (html_data.pay_order_index != data.result.page_count) {
                        html_data.pay_order_index += 1;
                    }
                    _(data.result.list).each(function (item) {
                        html_data.pay_order_list.push(item);
                        item['goods_num'] = 0;
                        _(item.goods_order_item_list).each(function (goods) {
                            item['goods_num'] += goods.num;
                        });
                    });
                });
            },
            query_goods_order_list: function () {
                api.lorder.query_goods_order_list({
                    status: '1',
                    goods_status: '0,1,2,3',
                    page_size: 200,
                    page_index: html_data.goods_order_index
                }).then(function (data) {
                    html_data.goods_order_index = data.result.page_index;
                    html_data.goods_order_count = data.result.page_count;
                    if (html_data.goods_order_index != data.result.page_count) {
                        html_data.goods_order_index += 1;
                    }
                    _(data.result.list).each(function (item) {
                        html_data.goods_order_list.push(item);
                        item['goods_num'] = 0;
                        _(item.goods_order_item_list).each(function (goods) {
                            item['goods_num'] += goods.num;
                        });
                    });
                });
            },

            make_goods_order: function (goods_order) {
                api.lorder.confirm_goods_received({goods_order_id: goods_order.id}).then(function (data) {
                    goods_order.goods_status = data.result.goods_status;
                    Magiclamp.mcAlert("提示", data.message);
                });
            },

            open_payorder: function (payorder_id) {
                location.href = 'pay.html?payorder_id=' + payorder_id;
            },
            open_order_info: function (order_id) {
                location.href = 'user_goods_orders.html?goods_order_id=' + order_id;
            },
            delete_payorder: function (payorder) {
                Magiclamp.mcConfirm("提示", "确认删除订单么？", null, function () {
                    api.lorder.delete_order({pay_order_id: payorder.id}).then(function (data) {
                        Magiclamp.mcAlert("提示", data.message);
                        payorder.is_active = false;

                        html_data.un_pay_order_list = _(html_data.un_pay_order_list).filter(function (item) {
                            return item.is_active;
                        });
                    });
                });

            },
            dispute_goods_order: function (goods_order) {
                Magiclamp.mcConfirm("提示", "确认投诉订单么？", null, function () {
                    api.lorder.dispute_goods_order({order_id: goods_order.id}).then(function (data) {
                        Magiclamp.mcAlert("提示", data.message);
                        goods_order.dispute_step = 1
                    })
                });
            },
            shengyudays: function (goods_order) {
                if (goods_order.goods_status == 4 || goods_order.goods_status == 0 || goods_order.goods_status == 1) {
                    return "";
                }
                if (goods_order.dispute_step == 0) {
                    //"received_date" "shipped_date"
                    if (goods_order.goods_status == 2) {
                        var shipped_date = moment(goods_order.shipped_date);
                        var end_date = moment(goods_order.shipped_date);
                        end_date.add(10, 'days');
                        var days = end_date.diff(shipped_date, 'days');
                        if (days == 0) {
                            return "(剩余：" + end_date.diff(shipped_date, 'hours') + "小时)"
                        } else {
                            return "(剩余：" + days + "天)";
                        }
                    } else if (goods_order.goods_status == 3) {
                        var shipped_date = moment(goods_order.received_date);
                        var end_date = moment(goods_order.received_date);
                        end_date.add(10, 'days');
                        var days = end_date.diff(shipped_date, 'days');
                        if (days == 0) {
                            return "(剩余：" + end_date.diff(shipped_date, 'days') + "小时)"
                        } else {
                            return "(剩余：" + days + "天)";
                        }
                    }

                } else {
                    return "";
                }
            },
            page_back: function () {
                baseConfig.page_back();
            },
            apply_for_arbitration: function (id) {
                api.lorder.create_order_dispute_record(id).then(function () {

                })
            }
        }
    });

    html_data.sessionid = GetQueryString('sessionid');
    if (html_data.sessionid) {
        html_data.sessionid_q = "sessionid=" + html_data.sessionid;
        client_storage('sessionid', html_data.sessionid);
    }

    app.query_un_pay_order_info_list();
    app.query_pay_order_info_list();
    app.query_goods_order_list();
    //
    //
    var $tags = $('#order-tab').find('a');
    window.mySwipe = new Swipe(document.getElementById('slider'), {
        continuous: false,
        disableScroll: false,
        stopPropagation: false,
        callback: function (index) {
            $tags.eq(index).addClass('tag-on')
                .siblings('a').removeClass('tag-on');
        },
        transitionEnd: function (index, elem) {
        }
    });
    $tags.eq(0).click(function (e) {
        mySwipe.slide(0);
        e.preventDefault();
    });
    $tags.eq(1).click(function (e) {
        mySwipe.slide(1);
        e.preventDefault();
    });
    $tags.eq(2).click(function (e) {
        mySwipe.slide(2);
        e.preventDefault();
    });
    //
    // /*动态更新订单状态*/
    //
    // var time_allow=5;//查询时间间隔 单位秒
    // var max_num=40;//查询多大次数
    // change_dn_type_send();
    // function change_dn_type_send(){
    //     var isgoing=false; //是否继续查询
    //     if(max_num==0){//次数够了不在查询
    //         return false;
    //     }
    //     Magiclamp.mcAjax('/account/member/getDnOrderTypeSendStatusByuserid?'+max_num,'POST','',function (data) {
    //         max_num--;
    //         var msg='';            
    //         if(data.status==0){//没有查询到数据 查询结束
    //             return false;
    //         }
    //         jQuery.each(data.data,function (k,v) {
    //             if(v==2){
    //                 $('#dn-other-info-'+k).prev('.dn-msg-before').remove();
    //                 msg='已派送';
    //             }else if(v==1){
    //                 $('#dn-other-info-'+k).prev('.dn-msg-before').remove();
    //                 msg='送货中';
    //             }else{
    //                 isgoing=true;///继续查询
    //                 msg='正在准备商品，请耐心等待' ;
    //             }
    //             $('#dn-other-info-'+k).find('span').eq(0).html(msg);
    //         });
    //         if(isgoing){
    //             setTimeout(change_dn_type_send,time_allow*1000);
    //         }
    //     })
    // }
    //
    // $('.get-more').click(function () {
    //     var pageNum = $('#page_num').val();
    //     console.log(pageNum);
    //     var url = "/account/member/user_order/" + (parseInt(pageNum) + 1);
    //     var pdata = {};
    //     var _this = $(this);
    //     Magiclamp.listLoad();
    //     Magiclamp.mcAjax(url, "post", pdata, function (dataBack) {
    //             var html = '';
    //             if (dataBack.data.orders) {
    //                 dataBack.data.orders.forEach(function (v) {
    //                     html += '<div class="order-item g-wrap">';
    //                     html += '<div class="order-title">';
    //                     html += '<h1>'+v.com_title+'</h1>';
    //                     html += '<p>'+v.stime+'</p>';
    //                     html += '</div>';
    //                     var mygoods = JSON.parse(v.mygoods);
    //                     mygoods.forEach(function(g){
    //                                 html += '<div class="g-item">';
    //                                 html += '<div class="g-item-wrap">';
    //                                 html += '<a href="#"  class="g-img">';
    //                                 html += '<img src="'+g.preimg+'">';
    //                                 html += '</a>';
    //                                 html += '<div class="g-i-info">';
    //                                 html += '<p class="g-i-name">'+g.title+'</p>';
    //                                 html += '<p class="g-i-count">';
    //                                 html += '<span class="g-i-sum">';
    //                                 html += '<span>￥</span><span class="price">'+(g.price/100).toFixed(2)+'</span>';
    //                                 html += '</span>';
    //                                 html += '<span class="reg-count tar">';
    //                                 html += 'x';
    //                                 html += '<i class="g-sum">'+g.counts+'</i>';
    //                                 html += '</span>';
    //                                 html += '</p>';
    //                                 html += '<p class="g-i-mark">';
    //                                 switch (g.order_type){
    //                                     case '0':
    //                                         html += '<i class="goods-mark mark-sd">售卖机自取</i>';
    //                                         break;
    //                                     case '1':
    //                                         html += '<i class="goods-mark mark-dn">前台送货</i>';
    //                                         break;
    //                                 }
    //                                 html += '</p>';
    //                                 html += '</div>';
    //                                 html += '</div>';
    //                                 html += '</div>';
    //                     })
    //                     html += '<div class="order-info">';
    //                     html += '<span class="order-room">';
    //                     if(1 == v.order_type && 'undefined' != v.door_num){
    //                         html += '房间号: <span>'+v.door_num+'</span>';
    //                         html += '<?php endif;?>';
    //
    //                     }
    //                      html += '</span>';
    //                      html += '<div class="order-price">';
    //                      html += '订单合计:';
    //                      html += '<span>￥</span><span class="price">'+(v.order_allprice/100).toFixed(2)+'</span>';
    //                      html += '</div></div></div>';
    //                 })
    //             }
    //             $('#get_more_finish').before(html);
    //             $('#page_num').val(dataBack.data.page);
    //             Magiclamp.listLoadGet($('#get_more_finish'), '加载更多', 'auto');
    //             if (parseInt(pageNum) == $('#page_num').val() || dataBack.data.orders.length != $('#page_limit').val()) {
    //                 Magiclamp.listLoadGet($('#get_more_finish'), '已加载全部', 'none');
    //             }
    //
    //         var html = '';
    //         if (dataBack.data.unfinish) {
    //             dataBack.data.unfinish.forEach(function (v) {
    //                 html += '<div class="order-item g-wrap">';
    //                 html += '<div class="order-title">';
    //                 html += '<h1>'+v.com_title+'</h1>';
    //                 html += '<p>'+v.stime+'</p>';
    //                 html += '</div>';
    //                 var mygoods = JSON.parse(v.mygoods);
    //                 mygoods.forEach(function(g){
    //                     html += '<div class="g-item">';
    //                     html += '<div class="g-item-wrap">';
    //                     html += '<a href="#"  class="g-img">';
    //                     html += '<img src="'+g.preimg+'">';
    //                     html += '</a>';
    //                     html += '<div class="g-i-info">';
    //                     html += '<p class="g-i-name">'+g.title+'</p>';
    //                     html += '<p class="g-i-count">';
    //                     html += '<span class="g-i-sum">';
    //                     html += '<span>￥</span><span class="price">'+(g.price/100).toFixed(2)+'</span>';
    //                     html += '</span>';
    //                     html += '<span class="reg-count tar">';
    //                     html += 'x';
    //                     html += '<i class="g-sum">'+g.counts+'</i>';
    //                     html += '</span>';
    //                     html += '</p>';
    //                     html += '<p class="g-i-mark">';
    //                     switch (g.order_type){
    //                         case '0':
    //                             html += '<i class="goods-mark mark-sd">售卖机自取</i>';
    //                             break;
    //                         case '1':
    //                             html += '<i class="goods-mark mark-dn">前台送货</i>';
    //                             break;
    //                     }
    //                     html += '</p>';
    //                     html += '</div>';
    //                     html += '</div>';
    //                     html += '</div>';
    //                 })
    //                 html += '<div class="order-info">';
    //                 html += '<span class="order-room">';
    //                 if(1 == v.order_type && 'undefined' != v.door_num){
    //                     html += '房间号: <span>'+v.door_num+'</span>';
    //                     html += '<?php endif;?>';
    //                     html += '</span>';
    //                 }
    //                 html += '<div class="order-price">';
    //                 html += '订单合计:';
    //                 html += '<span>￥</span><span class="price">'+(v.order_allprice/100).toFixed(2)+'</span>';
    //                 html += '</div></div>';
    //                 html += '<div class="order-info dn-info">';
    //                 html += '<div class="dn-msg-before" style="float:left;" ><span class="waiting"></span></div>';
    //                
    //                 html += '   <span class="dn-other-info" id="dn-other-info-'+v.order_id+'">';
    //                 if(v.type_send == 1){
    //                     html += '<span class="dn-order-state">送货中</span>';
    //                 }else if(v.type_send == 2) {
    //                     html += '<span class="dn-order-state">已派送</span>';                        
    //                 }else{
    //                     html += '<span class="dn-order-state">正在准备商品，请耐心等待</span>';
    //                 }
    //                 html += '</span>';
    //                 html += '</div>';
    //                 html += '</div>';
    //             })
    //         }
    //         $('#get_more_unfinish').before(html);
    //         $('#page_num').val(dataBack.data.page);
    //         Magiclamp.listLoadGet($('#get_more_unfinish'), '加载更多', 'auto');
    //         if (parseInt(pageNum) == $('#page_num').val() || dataBack.data.unfinish.length != $('#page_limit').val() ) {
    //             Magiclamp.listLoadGet($('#get_more_unfinish'), '已加载全部', 'none');
    //         }
    //             Magiclamp.imgSrc();
    //         }, function (databack) {
    //             Magiclamp.listLoadGet(_this, databack.message, 'none');
    //         }, true
    //     );
    //     return false;
    // });
    //
    //
    // $('#r_btn').on('click',function () {
    //     url=$(this).data('href');
    //     Magiclamp.mcAjax(url,'get','',function (data) {
    //         console.log(data);
    //         if(data.status==1){
    //             // location.reload();
    //         }else{                
    //             // location.reload();
    //         }
    //     },function () {
    //        
    //     })
    // })
});